package service.impl;

import dao.ArticleDao;
import dao.impl.AdminDaoImpl;
import dao.impl.ArticleDaoImpl;
import domain.*;
import service.ArticleService;

import java.util.List;
import java.util.Map;

public class ArticleServiceImpl implements ArticleService {
    private ArticleDao articleDao = new ArticleDaoImpl() ;

    @Override
    public PageBean pageBean(String currentPage_, String rows_, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        //1.创建一个空集合
        PageBean pb = new PageBean(); //后面也加上了泛型是为了向下兼容
        //2.调用dao查询总记录数
        int totalCount = articleDao.findTotalCount(condition);
        //3.计算总页码数
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        //4.安全检验,如果当前页小于1,跳转到最后一页,如果当前页大于总页数跳转到第一页
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        //5.计算开始查询的索引
        int start = (currentPage - 1) * rows;
        //6.调用dao查询list集合
        List<Article> list = articleDao.findByPage(start, rows, condition);
        //7.设置属性,为什么放在最后?
        //因为如果提前设置会导致PageBean中的的当前页会导致第四部要防止的问题失效
        //这个是因为PageBean的属性已经提前设置好了,而不是更新之后的
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        //8.返回PageBean对象
        return pb;
    }

    @Override
    public PageBean findLabelByLid(String currentPage_, String rows_, int lid) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        //1.创建一个空集合
        PageBean pb = new PageBean(); //后面也加上了泛型是为了向下兼容
        //2.调用dao查询总记录数
        int totalCount = articleDao.findLabelTotalCount(lid);
        //3.计算总页码数
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        //4.安全检验,如果当前页小于1,跳转到最后一页,如果当前页大于总页数跳转到第一页
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        //5.计算开始查询的索引
        int start = (currentPage - 1) * rows;
        //6.调用dao查询list集合
        List<ArticleLabel> list = articleDao.findLabelPageBean(start, rows, lid);
        //7.设置属性,为什么放在最后?
        //因为如果提前设置会导致PageBean中的的当前页会导致第四部要防止的问题失效
        //这个是因为PageBean的属性已经提前设置好了,而不是更新之后的
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        //8.返回PageBean对象
        return pb;

    }

    @Override
    public Article findArticleByRid(int rid) {
        return articleDao.findArticleByRid(rid);
    }

    @Override
    public PageBean findArticleByPage(String currentPage_, String rows_) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        PageBean pb = new PageBean();
        int totalCount = articleDao.findGoodArticleTotalCount();
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * rows;
        List<Article> list = articleDao.findByPageFromGoodArticle(start, rows);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        return pb;
    }

    @Override
    public User findUserByCode(String code) {
        AdminDaoImpl adminDao = new AdminDaoImpl();
        return adminDao.findUserByCode(code);
    }

    @Override
    public Category findCategory(int cid) {
        AdminDaoImpl adminDao = new AdminDaoImpl();
        return adminDao.findCategory(cid);
    }

    @Override
    public void addBrowserNum(int rid) {
        articleDao.addBrowserNumByRid(rid);
    }
}
